<?php


namespace app\general\controller;


use app\general\model\SqArticleHot;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Controller;
use think\Request;

class BeautifyArticle extends Controller
{
    protected $sq_article_hot;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->sq_article_hot = new SqArticleHot();
    }
    /**
     * 后台抓取
     */
    public function clicb(){
        set_time_limit(0);
        $article_type = $this->sq_article_hot->article_genre_new;
        $article_type_val=$this->sq_article_hot->article_genre_new_val;
        $types=array_keys($article_type);
        $is_true=false;
        foreach ($types as $k=>$v){
            $type=$v;
            $typename=$article_type[$v];
            $where=[
                "article_genre"=>$type
            ];
            $info=$this->sq_article_hot->field("pn")->where($where)->order("id desc")->find();
            if($info){
                $where=[
                    "article_genre"=>$article_type_val[$v],
                    "pn"=>$info["pn"]
                ];
                $count=$this->sq_article_hot->field("pn")->where($where)->count();
                if($count==10){
                    $pn=$info["pn"]+1;
                }else{
                    $pn=$info["pn"];
                }
            }else{
                $pn=1;
            }
            requests::$input_encoding = 'GB2312';
            requests::$output_encoding = 'GB2312';
            requests::set_useragent(array(
                "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/",
                "Opera/9.80 (Android 3.2.1; Linux; Opera Tablet/ADR-1109081720; U; ja) Presto/2.8.149 Version/11.10",
                "Mozilla/5.0 (Android; Linux armv7l; rv:9.0) Gecko/20111216 Firefox/9.0 Fennec/9.0"
            ));
            if($pn==1){
                if($k>6){
                   $url="http://www.szwj72.cn/Article/$v/Index.html";
                }else{
                    $url="http://www.szwj72.cn/essay/$v/index.html";
                }
            }else{
                if($k>6){
                    $url="http://www.szwj72.cn/Article/$v/index_$pn.html";
                }else{
                    $url="http://www.szwj72.cn/essay/$v/index_$pn.html";
                }

            }
            $html = requests::get($url);
            $selector = ".//div[@class='klyr-box']/ul/li[@class='index_list_title']/a/text()";
            //获取标题
            $title = selector::select($html, $selector,'xpath');
             //获取图片
            $selector = ".//div[@class='klyr-box']/ul/li[@class='info_thumb']/a/img/@src";
            $images=selector::select($html, $selector,'xpath');
            //文章内容地址
            $selector = ".//div[@class='klyr-box']/ul/li[@class='info_thumb']/a/@href";
            $urls=selector::select($html, $selector,'xpath');
            $id = $this->recordTimers(5);
            if(empty($ids)){
                $message=$typename."已无文章";
            }else{
                $flag=$this->getContents($urls,$title,$images,$type,$typename,$pn);
                $message=$flag?$typename."爬取文章成功":"爬取文章失败";
            }
            $this->updateTimerType($id,$message);
            if($flag){
                $is_true=true;
            }
        }
        return $is_true?$this->success("爬取成功"):$this->error("爬取失败");

    }
    //获取某个文章的内容
    public function getContents($urls,$title,$images,$type,$typename,$pn){
        requests::$input_encoding = 'utf-8';
        requests::$output_encoding = 'utf-8';
        //标记一次爬取一次
        $is_one=true;
        //标记是否爬取成功
        $success=false;
        $formdata=array();
        foreach ($urls as $k=>$v){
            $titles=$title[$k];
            $articleHot=new SqArticleHot();
            $where=[
                'title'=>$titles,
                'article_genre'=>array("gt",0)
            ];
            $info=$articleHot->where($where)->count();
            if($info<1){
                $html = requests::get($v);
                //获取内容
                $selector="//div[contains(@class,'blog-main')]";
                $content=selector::select($html, $selector,'xpath');
                if($content){
                    $data=array(
                        "title"=>$title[$k],
                        "article_genre"=>$type,
                        "article_type"=>0,
                        "thumb"=>$images[$k],
                        "source"=>"网络",
                        "author"=>"美文摘抄",
                        "content"=>$content,
                        "create_time"=>time(),
                        'pn'=>$pn
                    );
                    $formdata[]=$data;
                }
            }
        }
        $flag=$articleHot->saveAll($formdata);
        if($flag){
            $success=true;
        }
        return $success;
    }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }

}